package com.fh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fh.mapper.BrandMapper;
import com.fh.model.Brand;
import com.fh.service.BrandService;
import com.fh.util.DataTablePageUtil;
import com.fh.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public DataTablePageUtil<Brand> queryBrand(DataTablePageUtil<Brand> page, BrandVo brandVo) {
        //将条件查询放入json中
        JSONObject json = (JSONObject)JSONObject.toJSON(brandVo);
        //这是求分页的下标
        json.put("start",page.getStart());
        json.put("length",page.getLength());
        //查询数据
        List<Brand> brands = brandMapper.queryBrand(json);
        //查询数据个数
        Integer count = brandMapper.queryCount();
        page.setData(brands);
        page.setRecordsTotal(count);
        page.setRecordsFiltered(count);
        return page;
    }

    @Override
    public void addBrand(Brand brand) {
        brandMapper.addBrand(brand);
    }

    @Override
    public Brand toUpdateBrand(Integer brandId) {
        return brandMapper.toUpdateBrand(brandId);
    }

    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateBrand(brand);
    }

    @Override
    public void deleteBrand(Integer brandId) {
        brandMapper.deleteBrand(brandId);
    }

    @Override
    public void deleteBothBrand(Integer[] arr) {
        brandMapper.deleteBothBrand(arr);
    }
}
